package com.easy.frame.servers.sys.service.impl.type;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.frame.base.api.pojo.PageDTO;
import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.basic.constant.DbConstants;
import com.easy.frame.base.basic.enums.ResponseCodeEnum;
import com.easy.frame.base.basic.pojo.vo.DictContentVO;
import com.easy.frame.base.basic.util.AuthUtils;
import com.easy.frame.servers.sys.entity.SysBusinessType;
import com.easy.frame.servers.sys.entity.SysStaff;
import com.easy.frame.servers.sys.entity.SysTenant;
import com.easy.frame.servers.sys.mapper.staff.SysStaffMapper;
import com.easy.frame.servers.sys.mapper.tenant.SysTenantMapper;
import com.easy.frame.servers.sys.mapper.type.SysBusinessTypeMapper;
import com.easy.frame.servers.sys.mapping.SysBusinessTypeMapping;
import com.easy.frame.servers.sys.service.process.SysDictService;
import com.easy.frame.servers.sys.service.type.SysBusinessTypeService;
import com.easy.frame.servers.sys.vo.SysBusinessTypeVO;
import com.easy.frame.servers.sys.vo.SysDictVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务类型表 服务实现类
 *
 * @author tp
 * @since 2021-01-20
 */
@Service
@Slf4j
public class SysBusinessTypeServiceImpl extends ServiceImpl<SysBusinessTypeMapper, SysBusinessType> implements SysBusinessTypeService {

    @Autowired
    SysDictService sysDictService;

    @Resource
    SysBusinessTypeMapper sysBusinessTypeMapper;

    @Resource
    SysTenantMapper sysTenantMapper;

    @Resource
    SysStaffMapper sysStaffMapper;

    private static final String BUSINESS_TYPE = "BUSINESS_TYPE";

    @Override
    public List<SysBusinessTypeVO> selectForPage(PageDTO<SysBusinessType> param, Long tenantId, Integer isDisplay) {
        List<SysBusinessTypeVO> businessTypeList = sysBusinessTypeMapper.selectListVO(param.getParam(), tenantId, isDisplay);
        if (isDisplay == 0) {
            SysTenant sysTenant = new SysTenant();
            sysTenant.setId(tenantId);
            sysTenant.setAllAuth(DbConstants.DEFAULT_NO);
            sysTenantMapper.updateById(sysTenant);

            List<SysBusinessTypeVO> sysBusinessTypeList = new ArrayList<>();
            SysDictVO sysDictVO = sysDictService.selectByCode(BUSINESS_TYPE, DbConstants.LANGUAGE_CN);
            ArrayList<DictContentVO> content = sysDictVO.getContent();
            if (businessTypeList == null || businessTypeList.size() < content.size()) {
                if (businessTypeList != null) {
                    List<Long> collect = businessTypeList.stream().map(SysBusinessTypeVO::getId).collect(Collectors.toList());
                    sysBusinessTypeMapper.deleteBatchIds(collect);
                }
                content.forEach(s -> {
                    SysBusinessType sysBusinessType = new SysBusinessType();
                    sysBusinessType.setCode(String.valueOf(s.getKey()));
                    sysBusinessType.setName(s.getValue());
                    sysBusinessType.setCreateBy(AuthUtils.getCurrentUserId());
                    sysBusinessType.setUpdateBy(AuthUtils.getCurrentUserId());
                    sysBusinessType.setTenantId(tenantId);
                    sysBusinessType.setIsAuth(DbConstants.DEFAULT_YES);
                    sysBusinessTypeMapper.insert(sysBusinessType);
                    SysBusinessTypeVO sysBusinessTypeVO = SysBusinessTypeMapping.INSTANCE.PO2VO(sysBusinessType);
                    sysBusinessTypeVO.setIsAll(true);
                    SysStaff staff = sysStaffMapper.selectById(AuthUtils.getCurrentUserId());
                    sysBusinessTypeVO.setUpdateByName(staff.getName());
                    sysBusinessTypeList.add(sysBusinessTypeVO);
                });
                return pageVO(param, sysBusinessTypeList);
            }
        }
        return pageVO(param, businessTypeList);

    }

    @Override
    public List<SysBusinessType> selectByTenantId(Long tenantId) {
        return sysBusinessTypeMapper.selectList(new LambdaQueryWrapper<SysBusinessType>()
                .eq(SysBusinessType::getTenantId, tenantId));
    }

    @Override
    public SysBusinessTypeVO businessById(Long id) {
        return sysBusinessTypeMapper.businessById(id);
    }

    @Override
    public List<String> selectNameByCodes(List<String> businessTypeList) {
        return null;
    }

    @Override
    public ServiceResponse<List<SysBusinessType>> listTenant(Long tenantId) {
        List<SysBusinessType> list = list(new LambdaQueryWrapper<SysBusinessType>()
                .eq(SysBusinessType::getIsAuth, DbConstants.DEFAULT_YES)
                .eq(SysBusinessType::getTenantId, tenantId)
                .eq(SysBusinessType::getDeleted, DbConstants.DEFAULT_NO));
        return new ServiceResponse<>(list, ResponseCodeEnum.SUCCESS);
    }


    /**
     * list集合物理分页方法
     */
    public List<SysBusinessTypeVO> pageVO(PageDTO<?> param, List<SysBusinessTypeVO> list) {
        List<SysBusinessTypeVO> resultTypeList;
        //list集合物理分页，特殊情况使用，不推荐（并发情况下有安全隐患）
        double i = (double) list.size() / (double) param.getPageSize();
        int ceil = (int) Math.ceil(i);
        if (param.getPageNum() == ceil) {
            resultTypeList = list.size() <= param.getPageSize() ? list
                    : list.subList((param.getPageNum() - 1) * param.getPageSize(), list.size());
        } else if (param.getPageNum() > ceil) {
            resultTypeList = null;
        } else {
            resultTypeList = list.size() <= param.getPageSize() ? list
                    : list.subList((param.getPageNum() - 1) * param.getPageSize(),
                    param.getPageNum() * param.getPageSize());
        }
        return resultTypeList;
    }

}
